package com.atguigu.dga.governance.service.impl;

import com.atguigu.dga.constant.DgaConstant;
import com.atguigu.dga.governance.bean.GovernanceAssessTable;
import com.atguigu.dga.governance.bean.GovernanceType;
import com.atguigu.dga.governance.mapper.GovernanceAssessTableMapper;
import com.atguigu.dga.governance.service.GovernanceAssessTableService;
import com.atguigu.dga.governance.service.GovernanceTypeService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.hadoop.thirdparty.org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 表治理考评情况 服务实现类
 * </p>
 *
 * @author tubage
 * @since 2024-03-22
 */
@Service
@DS("dga")
public class GovernanceAssessTableServiceImpl extends ServiceImpl<GovernanceAssessTableMapper, GovernanceAssessTable> implements GovernanceAssessTableService {
    // TODO 计算表级分数
    @Autowired
    GovernanceTypeService governanceTypeService;
    @Override
    public void calcTableScore(String assessDate) {
        // TODO 0 幂等处理
        remove(
                new QueryWrapper<GovernanceAssessTable>()
                        .eq("assess_date", assessDate)
        );
        // TODO 1 mapper查询到表级分数
        List<GovernanceAssessTable> governanceAssessTableList = getBaseMapper().selectGovernanceAssessTableList(assessDate);

        // TODO 2 查询指标类型权重 记得装配
        // 遍历存到map结构
        List<GovernanceType> governanceTypeList = governanceTypeService.list();
        Map<String, BigDecimal> governanceTypeMap = new HashMap<>();
        for (GovernanceType governanceType : governanceTypeList) {
            governanceTypeMap.put(governanceType.getTypeCode(), governanceType.getTypeWeight());
        }

        // TODO 3 计算五维权重后的总分
        // dga定义常量
        for (GovernanceAssessTable governanceAssessTable : governanceAssessTableList) {
            BigDecimal specWeightScore = governanceAssessTable.getScoreSpecAvg().multiply(governanceTypeMap.get(DgaConstant.GOVERNANCE_TYPE_SPEC)).divide(BigDecimal.TEN);
            BigDecimal calcWeightScore = governanceAssessTable.getScoreCalcAvg().multiply(governanceTypeMap.get(DgaConstant.GOVERNANCE_TYPE_CALC)).divide(BigDecimal.TEN);
            BigDecimal qualityWeightScore = governanceAssessTable.getScoreQualityAvg().multiply(governanceTypeMap.get(DgaConstant.GOVERNANCE_TYPE_QUALITY)).divide(BigDecimal.TEN);
            BigDecimal storageWeightScore = governanceAssessTable.getScoreStorageAvg().multiply(governanceTypeMap.get(DgaConstant.GOVERNANCE_TYPE_STORAGE)).divide(BigDecimal.TEN);
            BigDecimal securityWeightScore = governanceAssessTable.getScoreSecurityAvg().multiply(governanceTypeMap.get(DgaConstant.GOVERNANCE_TYPE_SECURITY)).divide(BigDecimal.TEN);

            governanceAssessTable.setScoreOnTypeWeight(
                    specWeightScore.add(calcWeightScore).add(qualityWeightScore).add(securityWeightScore).add(storageWeightScore)
            );
        }

        // TODO 4 写回表中
        saveBatch(governanceAssessTableList);
    }
}
